#!/usr/bin/perl
#
$totalfreq=0;
foreach $line (<STDIN>) {
	my($left,$right);
	chomp($line);
	$line=~s/^\s*//g;
	($freq,$left,$right) = split(/\s+/,$line);
	$countBi{"$left"}->{"$right"}+=$freq;
	$count{"$left"}+=$freq;
	$totalfreq+=$freq;
}

$entropy = 0;
foreach $left (sort {$count{$b}<=>$count{$a}} keys %count) {
	$margProb = $count{$left}/$totalfreq;
	print STDERR "$left\t$margProb\n";

	$oneLeft = $countBi{$left};
	foreach $right (sort {$oneLeft->{$b}<=>$oneLeft->{$a}} keys %$oneLeft) {
		$prob = $countBi{$left}->{$right}/$count{$left};
		print $left."\t".$right."\t".$prob."\n";

		$entropy+= -1*$margProb*$prob*log($prob);
	}
}

print STDERR "ENTROPY = $entropy\n";
